<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!--:model:要验证的表单数据校验对象 :rules:表单校验规则 prop:要校验字段的属性名-->
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input type="text" :prefix-icon="User" placeholder="请输入用户名"
                      v-model="loginForm.username"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" placeholder="请输入密码" v-model="loginForm.password"
                      show-password></el-input>
          </el-form-item>

          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
//引入element-plus提示框
import { ElNotification } from 'element-plus'
//引入用户相关的小仓库
import useUserStore from '@/store/modules/user.ts'
//引入获取当前时间的函数
import { getTime } from '@/utils/time.ts'

//获取路由
let $router = useRouter()
//获取路由对象
let $route = useRoute()
//定义变量控制按钮加载效果
let loading = ref(false)
let useStore = useUserStore()
//收集账号与密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })
//通过ref属性获取el-form组件
let loginForms = ref()
const login = async () => {
  //保证全部的表单项校验通过再发请求
  await loginForms.value.validate()
  // 加载效果：开始加载
  loading.value = true
  //点击登录按钮以后干什么?
  //通知仓库发登录请求
  //请求成功->首页展示数据的地方
  //请求失败->弹出登录失败信息
  try {
    // 保证登录成功
    await useStore.userLogin(loginForm)
    //判断登录的时候，路由的路径当中是否有query参数，如果有就往query参数跳转，没有就跳转到首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // 编程式导航跳转到展示数据首页
    $router.push('/')
    // 登录成功信息提示
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })
  } catch (error) {
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  } finally {
    // 登录成功/失败加载效果消失
    loading.value = false
  }
}

//定义表单校验配置对象
const rules = {
  username: [
    { required: true, min: 5, max: 10, message: '用户名长度应为5-10位', trigger: 'change' },
  ],
  password: [
    { required: true, min: 6, max: 10, message: '密码长度应为5-10位', trigger: 'change' },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg");
  background-size: cover;
}

.login_form {
  position: relative;
  width: 70%;
  top: 30vh;
  background: url("@/assets/images/login_form.png");
  background-size: cover;
}

h1 {
  color: white;
  font-size: 40px
}

h2 {
  color: white;
  font-size: 25px;
  margin: 20px 0;
}
</style>